/**
 * Copyright 2012 ibiblio
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ibiblio.terasaur.ui

import org.ibiblio.terasaur.ui.StringCleaner
import org.ibiblio.terasaur.item.Item
import org.ibiblio.terasaur.item.Collection
import org.ibiblio.terasaur.bt.Torrent
import org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib
import org.codehaus.groovy.grails.commons.ConfigurationHolder

class UrlHelper {
    private static _stringCleaner = new StringCleaner()
    private g

    public UrlHelper() {
        g = new ApplicationTagLib()
    }

    public String canonicalLink(Item item, String action) {
        if (!item) {
            return ''
        }
        String urlTitle = _stringCleaner.clean(item.title)
        String link = g.createLink([controller:'item', action:action, id:item.id, params:[title: urlTitle]]).toString()
        return link
    }

    protected String canonicalLink(Torrent torrent) {
        if (!torrent) {
            return ''
        }
        String urlName = _stringCleaner.clean(torrent.name)
        String link = g.createLink([controller:'torrent', action:'show', id:torrent.infoHash, params:[name: urlName]]).toString()
        return link
    }

    /**
     * Generate canonical collection link for given action
     *
     * @param collection
     * @param action
     * @return
     */
    public String collectionLink(Collection collection, String action) {
        if (!collection || !action) {
            return ''
        }
        return collectionLink(collection.id, collection.name, action)
    }


    /**
     * Generate canonical collection link for given action
     *
     * @param collectionId
     * @param collectionName
     * @param action
     * @return
     */
    public String collectionLink(Long collectionId, String collectionName, String action) {
        if (!collectionId || !collectionName || !action) {
            return ''
        }

        String urlName = _stringCleaner.clean(collectionName)
        String link

        // Reverse order for search and browse actions
        if (action == 'search' || action == 'browse') {
            link = g.createLink([mapping:action + 'Collection', controller:action, action:'collection', id:collectionId, params:[name: urlName]]).toString()
        } else {
            link = g.createLink([controller:'collection', action:action, id:collectionId]).toString()
        }
        return link
    }

    public String serverUrl() {
        return ConfigurationHolder.config.grails.serverURL
    }

    public String serverUrl(request) {
        def url = 'http://' + request.serverName
        if (request.serverPort != 80) {
            url += ':' + request.serverPort.toString()
        }
        return url
    }
}
